package com.production.CoursePlatform.controller;

import com.production.CoursePlatform.entity.Studying;
import com.production.CoursePlatform.service.StudyingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/studying")
public class StudyingController {

    @Autowired
    private StudyingService studyingService;

    // 添加学习记录
    @PostMapping("/add")
    public ResponseEntity<String> addStudying(@RequestBody Studying studying) {
        int result = studyingService.addStudying(studying);
        return result > 0 ? ResponseEntity.ok("添加成功") : ResponseEntity.status(500).body("添加失败");
    }

    // 根据 `cts_id` 删除学习记录
    @DeleteMapping("/delete/{cts_id}")
    public ResponseEntity<String> deleteStudying(@PathVariable int cts_id) {
        int result = studyingService.deleteStudying(cts_id);
        return result > 0 ? ResponseEntity.ok("删除成功") : ResponseEntity.status(500).body("删除失败");
    }

    // 更新学习记录
    @PutMapping("/update")
    public ResponseEntity<String> updateStudying(@RequestBody Studying studying) {
        int result = studyingService.updateStudying(studying);
        return result > 0 ? ResponseEntity.ok("更新成功") : ResponseEntity.status(500).body("更新失败");
    }

    // 根据 `s_id` 查找学习记录
    @GetMapping("/find/{s_id}")
    public ResponseEntity<List<Studying>> getStudyingBySID(@PathVariable String s_id) {
        List<Studying> studyingList = studyingService.getStudyingBySID(s_id);
        return studyingList != null && !studyingList.isEmpty()
                ? ResponseEntity.ok(studyingList)
                : ResponseEntity.status(404).body(null);
    }

    // 获取所有学习记录
    @GetMapping("/all")
    public ResponseEntity<List<Studying>> getAllStudyings() {
        List<Studying> studyingList = studyingService.getAllStudyings();
        return studyingList.isEmpty()
                ? ResponseEntity.status(404).body(null)
                : ResponseEntity.ok(studyingList);
    }
}

